/*
   Copyright (C) 2008 Yen-Ju Chen <yjchenx gmail>

   This application is free software; you can redistribute it and/or 
   modify it under the terms of the MIT license. See COPYING.

*/

#import "GNUStep.h"
#import "SequenceView.h"
#import "ICItem.h"
#import "BioCocoa/BCFoundation.h"
#import "BioCocoa/BCAppKit.h"

@implementation SequenceView

- (void) awakeFromNib
{
	if (item == nil)
	{
		[typeButton setEnabled: NO];
	}
}

- (IBAction) editAction: (id) sender
{
	if ([sender state] == NSOnState)
	{
		[sequenceView setBackgroundColor: [NSColor yellowColor]];
		[sequenceView setEditable: YES];
	}
	else
	{
		[sequenceView setBackgroundColor: [NSColor whiteColor]];
		[sequenceView setEditable: NO];
		/* Finish editing. Store sequence back */
		NSString *s = [sequenceView string];
		[item setDNASequence: [BCSequence sequenceWithString: s]];
		if ([delegate respondsToSelector: @selector(sequenceViewDidFinishEditing:)])
		{
			[delegate sequenceViewDidFinishEditing: self];
		}
	}
}

- (void) typeAction: (id) sender
{
	[item setType: [typeButton indexOfSelectedItem]];

	if ([delegate respondsToSelector: @selector(typeDidChangeInSequenceView:)])
	{
		[delegate typeDidChangeInSequenceView: self];
	}
}

- (NSView *) view
{
	return view;
}


- (void) setItem: (ICItem *) i
{
	ASSIGN(item, i);

	/* If NSTextView is not editable, -setString: has no effect */
	BOOL isEditable = [sequenceView isEditable];
	if ([sequenceView isEditable] == NO)
		[sequenceView setEditable: YES];

	if (item == nil)
	{
		[typeButton setEnabled: NO];
		[sequenceView setString: @""];
	}
	else
	{
		[typeButton setEnabled: YES];
		[typeButton selectItemAtIndex: [item type]];
		[sequenceView setString: [[item DNASequence] sequenceString]];
	}

	[sequenceView setEditable: isEditable];
}

- (ICItem *) item
{
	return item;
}

- (void) setDelegate: (id) d
{
	ASSIGN(delegate, d);
}

- (id) delegate
{
	return delegate;
}

@end
